package com.skhu.as.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.skhu.as.domain.PresentCondition;
import com.skhu.as.service.PresentConditionS;
import com.skhu.as.util.DateU;

public class PresentConditionC extends AbstractController {
	
	private String successView;
	private PresentConditionS presentConditionS;
	
	public void setSuccessView(String successView) {
		this.successView = successView;
	}
	public void setPresentConditionS(PresentConditionS presentConditionS) {
		this.presentConditionS = presentConditionS;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = null;
		String mode = ServletRequestUtils.getStringParameter(request, "mode", "");
		
		
		if(mode.equals("insert")){
			mav = insertPresentCondition(request, response);
		}else{
			mav = deletePresentCondition(request, response);
		}
		
		return mav;
	}
	
	private ModelAndView insertPresentCondition(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		DateU dateU = new DateU(); 
		
		int presentConditionNo = presentConditionS.getPresentConditionMaxNo() + 1;
		int faultHandlingNo = ServletRequestUtils.getIntParameter(request, "faultHandlingNo");
		int workerNo = ServletRequestUtils.getIntParameter(request, "workerNo");
		String content = ServletRequestUtils.getStringParameter(request, "content", "");
		String date = dateU.getDateTime();
		
		String year = ServletRequestUtils.getStringParameter(request, "year", "");
		String month = ServletRequestUtils.getStringParameter(request, "month", "");
		String dayOfMonth = ServletRequestUtils.getStringParameter(request, "dayOfMonth", "");
		
		PresentCondition presentCondition = new PresentCondition();
		presentCondition.setContent(content);
		presentCondition.setDate(date);
		presentCondition.setFaultHandlingNo(faultHandlingNo);
		presentCondition.setPresentConditionNo(presentConditionNo);
		presentCondition.setWorkerNo(workerNo);
		
		ModelAndView mav = new ModelAndView(successView + "?year=" + year + "&month=" + month + "&dayOfMonth=" + dayOfMonth + "&faultHandlingNo=" + faultHandlingNo);
		int result = presentConditionS.insertPresentCondition(presentCondition);
		
		return mav;
	}
	
	private ModelAndView deletePresentCondition(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DateU dateU = new DateU(); 
		
		int presentConditionNo = ServletRequestUtils.getIntParameter(request, "presentConditionNo");
		int faultHandlingNo = ServletRequestUtils.getIntParameter(request, "faultHandlingNo");
		
		String year = ServletRequestUtils.getStringParameter(request, "year", "");
		String month = ServletRequestUtils.getStringParameter(request, "month", "");
		String dayOfMonth = ServletRequestUtils.getStringParameter(request, "dayOfMonth", "");
		
		ModelAndView mav = new ModelAndView(successView + "?year=" + year + "&month=" + month + "&dayOfMonth=" + dayOfMonth + "&faultHandlingNo=" + faultHandlingNo);
		
		int result = presentConditionS.deletePresentCondition(presentConditionNo);
		
		return mav;
	}
	
}
